<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-cascader v-model="idcCabinetRackQuery" :options="rackOptions" clearable filterable style="width: 220px; margin-right: 10px;" class="filter-item" change-on-select placeholder="请选择机房/机柜"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" element-loading-text="加载中" border fit highlight-current-row style="width: 100%">
      <el-table-column :label="$t('table.id')" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" align="center" label="设备编号">
        <template slot-scope="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="所属机柜">
        <template slot-scope="scope">
          <span>{{ scope.row.rackName }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" align="center" label="设备型号">
        <template slot-scope="scope">
          <span>{{ scope.row.model }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" align="center" label="设备类型">
        <template slot-scope="scope">
          <span>{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="上架时间">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.comment')" min-width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.comment }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.createTime')" width="180px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.creator')" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.creator }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.updateTime')" width="180px">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.updator')" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.updator }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" fixed="right" width="160" >
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">{{ $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="机柜" prop="idcCabinetRack">
          <el-cascader v-model="idcCabinetRack" :options="rackOptions" placeholder="请选择机房/机柜" @change="cabinetRackChange"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="type">
          <el-select v-model="temp.type" class="filter-item" placeholder="请选择设备类型">
            <el-option v-for="item in typeOptions" :key="item.id" :label="item.label" :value="item.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="temp.model"/>
        </el-form-item>
        <el-form-item label="设备编号" prop="code">
          <el-input v-model="temp.code"/>
        </el-form-item>
        <el-form-item label="上架时间" prop="startTime">
          <el-date-picker v-model="temp.startTime" type="datetime" placeholder="选择上架时间"/>
        </el-form-item>
        <el-form-item label="U数" prop="udesc">
          <el-input v-model="temp.udesc" :min="1" :max="10"/>
        </el-form-item>
        <el-form-item label="U的位置" prop="uposition">
          <el-input v-model="temp.uposition" :min="1" :max="60"/>
        </el-form-item>
        <el-form-item label="用途" prop="comment">
          <el-input :autosize="{ minRows: 2, maxRows: 4}" v-model="temp.comment" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{ $t('table.confirm') }}</el-button>
        <el-button v-else type="primary" @click="updateData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchList,
  fetchRackOptions,
  create,
  update,
  del
} from '@/api/networkDevice'

export default {
  name: 'ComplexTable',
  filters: {},
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        cabinetId: '',
        idcId: '',
        rackId: '',
        sort: '+id'
      },
      typeOptions: [
        { id: 1, label: '交换机', name: 'switch' },
        { id: 2, label: '路由器', name: 'router' }
      ],
      rackOptions: [],
      idcCabinetRack: [],
      idcCabinetRackQuery: [],
      temp: {
        id: undefined,
        code: '', // 设备编号
        model: '', // 设备型号
        type: '', // 设备类型
        startTime: '', // 上架时间
        cabinetId: '', // 机柜ID
        idcId: '', // 机房ID
        rackId: '', // 机柜ID
        udesc: '', // U数
        uposition: '', // U的位置
        comment: '' // 功能
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        idcCabinetRack: [
          { required: false, message: '机柜是必填项', type: 'array', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '设备类型是必填项', trigger: 'blur' }
        ],
        model: [
          { required: true, message: '设备型号是必填项', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '设备编号必填项', trigger: 'blur' }
        ]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
    this.getRackOptions()
  },
  methods: {
    // 获取设备列表
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
        // this.listQuery.idcId = "";
        // this.listQuery.cabinetId = "";
        // this.listQuery.rackId = "";
      })
    },
    // 获取机柜
    getRackOptions() {
      fetchRackOptions().then(response => {
        this.rackOptions = response.data
      })
    },
    // 筛选搜索
    handleFilter() {
      if (this.idcCabinetRackQuery.length === 0) {
        this.listQuery.idcId = ''
        this.listQuery.cabinetId = ''
      } else if (this.idcCabinetRackQuery.length === 1) {
        this.listQuery.idcId = this.idcCabinetRackQuery[0]
      } else if (this.idcCabinetRackQuery.length === 2) {
        this.listQuery.idcId = this.idcCabinetRackQuery[0]
        this.listQuery.cabinetId = this.idcCabinetRackQuery[1]
      } else if (this.idcCabinetRackQuery.length === 3) {
        this.listQuery.idcId = this.idcCabinetRackQuery[0]
        this.listQuery.cabinetId = this.idcCabinetRackQuery[1]
        this.listQuery.rackId = this.idcCabinetRackQuery[2]
      }
      this.listQuery.page = 1
      this.getList()
    },
    // 更改每页条数
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    // 更改当前页码
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    // 删除网络设备数据
    handleDelete(row) {
      this.$confirm('此删除操作不可还原, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del(row).then(response => {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(() => {})
    },
    // 重置数据
    resetTemp() {
      this.idcCabinetRack = []
      this.temp = {
        id: undefined,
        name: '',
        comment: ''
      }
    },
    cabinetRackChange(value) {
      console.log(value)
    },
    // 添加网络设备数据
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 提交网络设备数据
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.temp.idcId = this.idcCabinetRack[0]
          this.temp.cabinetId = this.idcCabinetRack[1]
          this.temp.rackId = this.idcCabinetRack[2]
          const tempData = Object.assign({}, this.temp)
          create(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    // 编辑网络设备数据
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.idcCabinetRack[0] = row.idcId
      this.idcCabinetRack[1] = row.cabinetId
      // this.idcCabinetRack[2] =  row.rackId;
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 提交已编辑数据
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.temp.idcId = this.idcCabinetRack[0]
          this.temp.cabinetId = this.idcCabinetRack[1]
          this.temp.rackId = this.idcCabinetRack[2]
          const tempData = Object.assign({}, this.temp)
          update(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
